package com.ruxiaoxin.share.util;

/***
 * 
 * @author laigood
 * 
 */
public class ShortLinkUtils {

	final static String[] SHORT_LINK_CHARS = new String[] { "a", "b", "c", "d", "e", "f", "g", "h",

															"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
														
															"u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
														
															"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",
														
															"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
														
															"U", "V", "W", "X", "Y", "Z"
											};

	/***
	 * 
	 * @param url
	 * @return
	 */
	public static String shortUrl(String url) {

		String key = "wuguowei";

		String sMD5EncryptResult = MD5Utils.MD5Encode(key + url);
		String hex = sMD5EncryptResult;
			
		String sTempSubString1 = hex.substring(0,5);
		String sTempSubString2 = hex.substring(5,10);
			
		long lHexLong1 = 0x0000FFFF & Long.parseLong(sTempSubString1, 16);
		long lHexLong2 = 0x0000FFFF & Long.parseLong(sTempSubString2, 16);
		String outChars = "";
		
		for (int j = 0; j < 4; j++) {
			long index = 0x0000003D & lHexLong1;
			outChars += SHORT_LINK_CHARS[(int) index];
			lHexLong1 = lHexLong1 >> 5;
		}
		for (int j = 0; j < 4; j++) {
			long index = 0x0000003D & lHexLong2;
			outChars += SHORT_LINK_CHARS[(int) index];
			lHexLong2 = lHexLong2 >> 5;
		}
		return outChars;
	}
	public static void main(String[] args) {
		System.out.println(shortUrl("http://www.google.com.hk/search?hl=en&newwindow=1&safe=active&tbs=sbi:AMhZZiu8-k5G519Wa0h8qeet8y65CotU_1OWrBQoEr2F7WGzyEEepxAbbreXDQwywplS3vsdAZxqBkQYrtAA6OmSJvnVuqVrH1ptj_1ZVcBLwcGWQf-ba0L_1Wkw4AsESk-HOqA7BbknSHg4bKPaSTQgC_1IEOCE2m8fpI55UYyu9yOeywRKqP3r6XNuOG36QTcx0t-gA32dKM2aiMEXRNgoHJx_18VK86EY_1bkCLim7OaZuP2v-IsJtIm1sDDyCW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